package com.lianjia.analysis.controller;

import com.github.abel533.echarts.Option;
import com.lianjia.analysis.model.HouseInfo;
import com.lianjia.analysis.service.HousePriceAnalysisService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * 房价分析控制器
 */
@RestController
@RequestMapping("/house")
@CrossOrigin
public class HousePriceAnalysisController {

    @Autowired
    private HousePriceAnalysisService housePriceAnalysisService;

    /**
     * 获取所有城市列表
     */
    @GetMapping("/cities")
    public ResponseEntity<List<String>> getAllCities() {
        return ResponseEntity.ok(housePriceAnalysisService.getAllCities());
    }

    /**
     * 获取城市房源数量统计
     */
    @GetMapping("/city/count")
    public ResponseEntity<Map<String, Integer>> getCityHouseCount() {
        return ResponseEntity.ok(housePriceAnalysisService.getCityHouseCount());
    }

    /**
     * 获取城市房价均价统计
     */
    @GetMapping("/city/avg-price")
    public ResponseEntity<Map<String, Double>> getCityAveragePrice() {
        return ResponseEntity.ok(housePriceAnalysisService.getCityAveragePrice());
    }

    /**
     * 获取城市房价中位数统计
     */
    @GetMapping("/city/median-price")
    public ResponseEntity<Map<String, Double>> getCityMedianPrice() {
        return ResponseEntity.ok(housePriceAnalysisService.getCityMedianPrice());
    }

    /**
     * 获取城市房价区间分布
     */
    @GetMapping("/city/price-distribution")
    public ResponseEntity<Map<String, Map<String, Integer>>> getCityPriceDistribution() {
        return ResponseEntity.ok(housePriceAnalysisService.getCityPriceDistribution());
    }

    /**
     * 获取城市面积区间分布
     */
    @GetMapping("/city/area-distribution")
    public ResponseEntity<Map<String, Map<String, Integer>>> getCityAreaDistribution() {
        return ResponseEntity.ok(housePriceAnalysisService.getCityAreaDistribution());
    }

    /**
     * 获取城市户型分布
     */
    @GetMapping("/city/layout-distribution")
    public ResponseEntity<Map<String, Map<String, Integer>>> getCityLayoutDistribution() {
        return ResponseEntity.ok(housePriceAnalysisService.getCityLayoutDistribution());
    }

    /**
     * 获取城市朝向分布
     */
    @GetMapping("/city/orientation-distribution")
    public ResponseEntity<Map<String, Map<String, Integer>>> getCityOrientationDistribution() {
        return ResponseEntity.ok(housePriceAnalysisService.getCityOrientationDistribution());
    }

    /**
     * 获取城市装修情况分布
     */
    @GetMapping("/city/decoration-distribution")
    public ResponseEntity<Map<String, Map<String, Integer>>> getCityDecorationDistribution() {
        return ResponseEntity.ok(housePriceAnalysisService.getCityDecorationDistribution());
    }

    /**
     * 获取城市楼层分布
     */
    @GetMapping("/city/floor-distribution")
    public ResponseEntity<Map<String, Map<String, Integer>>> getCityFloorDistribution() {
        return ResponseEntity.ok(housePriceAnalysisService.getCityFloorDistribution());
    }

    /**
     * 获取价格与面积散点图数据
     */
    @GetMapping("/city/{cityName}/price-area-scatter")
    public ResponseEntity<Option> getPriceAreaScatterData(@PathVariable String cityName) {
        return ResponseEntity.ok(housePriceAnalysisService.getPriceAreaScatterData(cityName));
    }

    /**
     * 获取城市各区域均价排行
     */
    @GetMapping("/city/{cityName}/district-price-ranking")
    public ResponseEntity<Option> getCityDistrictPriceRanking(@PathVariable String cityName) {
        return ResponseEntity.ok(housePriceAnalysisService.getCityDistrictPriceRanking(cityName));
    }

    /**
     * 获取城市价格趋势（按月）
     */
    @GetMapping("/city/{cityName}/price-trend")
    public ResponseEntity<Option> getCityPriceTrend(@PathVariable String cityName) {
        return ResponseEntity.ok(housePriceAnalysisService.getCityPriceTrend(cityName));
    }

    /**
     * 根据条件查询房源信息
     */
    @GetMapping("/search")
    public ResponseEntity<List<HouseInfo>> searchHouses(
            @RequestParam(required = false) String cityName,
            @RequestParam(required = false) Double minPrice,
            @RequestParam(required = false) Double maxPrice,
            @RequestParam(required = false) Double minArea,
            @RequestParam(required = false) Double maxArea,
            @RequestParam(required = false) String layout,
            @RequestParam(required = false) String location) {
        return ResponseEntity.ok(housePriceAnalysisService.searchHouses(
                cityName, minPrice, maxPrice, minArea, maxArea, layout, location));
    }
} 